package cn.mystyle.account.server.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import cn.mystyle.account.server.service.RecordService;
import cn.mystyle.common.bean.BaseController;
import cn.mystyle.model.account.Record;

/**
 * @author clark
 * 记一笔账相关业务
 */
@RestController
@RequestMapping(value = "/record")
public class RecordController extends BaseController
{
    /**
     * 记账业务类
     */
    @Autowired
    private RecordService recordService;
    
    /**
     * 查询记录
     * @param request HTTP请求体
     * @param response HTTP响应体
     * @param jsonData 请求参数
     * @return String
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public String query(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData)
    {
        Record record = JSON.parseObject(jsonData, Record.class);
        
        return recordService.queryRecord(record).toString();
    }
    
    /**
     * 记一笔
     * @param request HTTP请求体
     * @param response HTTP响应体
     * @param jsonData 请求参数
     * @return String
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData)
    {
        Record record = JSON.parseObject(jsonData, Record.class);
        
        return recordService.insertRecord(record).toString();
    }
    
    /**
     * 删除一笔
     * @param request HTTP请求体
     * @param response HTTP响应体
     * @param jsonData 请求参数
     * @return String
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public String del(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData)
    {
        Record record = JSON.parseObject(jsonData, Record.class);
        
        return recordService.deleteRecord(record).toString();
    }
    
    /**
     * 更新一笔
     * @param request HTTP请求体
     * @param response HTTP响应体
     * @param jsonData 请求参数
     * @return String
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData)
    {
        Record record = JSON.parseObject(jsonData, Record.class);
        
        return recordService.updateRecord(record).toString();
    }
}
